package com.fth.learn.chapter03;

/**
 * 快乐数
 * YiMi
 *
 * @author YiMi
 * @version 1.0
 * @date 2023/5/13 15:47
 */

public class HappyNum {

    int a = 19;

    public  static  boolean isHappy(int a){
        int n = 0;
        while (a!=1 && n<10000){

            int len = count(a);
            int temp = 0;
            for(int i = 0;i<len ; i++){
                int a1 = a/(int)(Math.pow(10,i))%10;
                temp += a1 *a1;
            }

//            int f = a %10;
//            int s = a / 10 % 10;
//            int t = a / 100 % 10;
//            a = f*f + s*s + t*t;
//            n++;
            a = temp;
            if(n++ >= 100){
                return false;
            }
        }
        return  true;

    }

    //求多少位
    public static int count(int n) {
        int index = 0;
        while (n!=0){
            n /=10;
            index++;
        }
        return index;
    }




    public static void main(String[] args) {
        System.out.println(isHappy(654));

//        System.out.println(count(12345645));
    }
}
